package cn.lecosa.util;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * 
 * 功能描述:动态读取配置文件来加载属性，或更改属性文件内容
 * 
 * 修改日期：2009-10-9
 */
public class PropertyUtil {
	private static final String config = "/jdbc.properties";

	/**
	 * 
	 * 方法用途和描述: 获得属性文件的属性
	 * 
	 * @param propertyFilePath
	 *            属性文件(包括类路径)
	 * @return 属性
	 */
	public final static Properties getProperties(String propertyFilePath) {
		Properties prop = new Properties();
		InputStream in = null;
		try {
			in = new BufferedInputStream(new FileInputStream(propertyFilePath));
			prop.load(new InputStreamReader(in, "utf-8"));

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return prop;
	}

	private  String readProp(String fileName,String propertyname) throws Exception {
		String value ="";
		String url =fileName;
		Properties prop= new Properties();
		try {
			InputStream is =this.getClass().getResourceAsStream(url);
			prop.load(is);
			value=prop.getProperty(propertyname );
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;

	}
	
	public static String getProp(String propertyname ) throws Exception{
		String value ="";
		try {
			value =new PropertyUtil().readProp(config,propertyname);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return value;
	}
	public static void main(String[] args) throws Exception {
		System.out.println(PropertyUtil.getProp("jdbc.driver"));
//		Properties params = PropertyUtil.getProperties("D:/spark/workspace/cq_bigdata/resources/application.conf");
//		System.out.println(params.getProperty("spark.bzh.hdfs.basePath"));

	}
}
